package com.salefinder.server.server;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.salefinder.client.SaleFinderService;
import com.salefinder.client.vo._ConsumerProductVO;
import com.salefinder.client.vo._NameIdPairVO;
import com.salefinder.client.vo._UserItemListVO;
import com.salefinder.client.vo._UserVO;
import com.salefinder.server.server.bd.SaleFinderBD;
import com.salefinder.server.server.exception.DataNotFoundException;
import com.salefinder.server.server.exception.SystemException;
import com.salefinder.server.server.util.Mapper;
import com.salefinder.server.server.vo.Brand;
import com.salefinder.server.server.vo.ConsumerProduct;
import com.salefinder.server.server.vo.Location;
import com.salefinder.server.server.vo.Merchant;
import com.salefinder.server.shared.FieldVerifier;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class SaleFinderServiceImpl extends RemoteServiceServlet implements
		SaleFinderService {

	
	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		// Escape data from the client to avoid cross-site script vulnerabilities.
		input = escapeHtml(input);
		userAgent = escapeHtml(userAgent);

		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	@Override
	public List<_ConsumerProductVO> searchProduct(_ConsumerProductVO vo) throws SystemException  {
		SaleFinderBD bd = SaleFinderBD.getInstance();
		ConsumerProduct svo = Mapper.toServerConsumerProduct(vo);
		return Mapper.toClientConsumerProductList( bd.searchCheapestConsumerProducts(svo));
		
		
	}

	@Override
	public _UserVO login(_UserVO vo) throws DataNotFoundException, SystemException {
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		return Mapper.toClientUser(bd.login(Mapper.toServerUser(vo)));
	}
	
	@Override
	public void submitProduct(_ConsumerProductVO vo) {
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		bd.createConsumerProduct(Mapper.toServerConsumerProduct(vo));
	}
	
	@Override
	public void registerUser(_UserVO vo) {
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		bd.registerUser(Mapper.toServerUser(vo));
	}
	
	@Override
	public List<_ConsumerProductVO> getMyList(_UserVO vo){
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		return Mapper.toClientUserItemList(bd.getMyItemList(Mapper.toServerUser(vo))).getConsumerProducts();
	}
	
	@Override
	public List<String> getProductSuggestions(String name) throws SystemException{
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		return bd.getProductSuggestions(name);
	
	}
	
	@Override
	public List<_NameIdPairVO> getBrandSuggestions(String name) throws SystemException{
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		List<_NameIdPairVO> clist = new ArrayList<_NameIdPairVO>(); 
		List<Brand> slist = bd.getBrandSuggestions(name);
		for(int i = 0 ; i < slist.size(); i++){
			clist.add(Mapper.brandToNameId(slist.get(i)));
		}
		return clist;	
	}
	
	@Override
	public List<_NameIdPairVO> getMerchantSuggestions(String name) throws SystemException{
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		List<_NameIdPairVO> clist = new ArrayList<_NameIdPairVO>(); 
		List<Merchant> slist = bd.getMerchantSuggestions(name);
		for(int i = 0 ; i < slist.size(); i++){
			clist.add(Mapper.merchantToNameId(slist.get(i)));
		}
		return clist;	
	}
	
	@Override
	public List<_NameIdPairVO> getLocationSuggestions(String name) throws SystemException{
		SaleFinderBD bd =  SaleFinderBD.getInstance();
		List<_NameIdPairVO> clist = new ArrayList<_NameIdPairVO>(); 
		List<Location> slist = bd.getLocationSuggestions(name);
		for(int i = 0 ; i < slist.size(); i++){
			clist.add(Mapper.locationToNameId(slist.get(i)));
		}
		return clist;	
	}
	
}
